package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    //添加订单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        //打印订单数据
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    //订单明细
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,Long number,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        //@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        //创建分页条件
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Orders> olqw = new LambdaQueryWrapper<>();
        olqw.between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime);
        olqw.eq(number!=null,Orders::getNumber,number);
        orderService.page(ordersPage, olqw);
        return R.success(ordersPage);
    }
    @GetMapping("/userPage")
    public R<Page>  userPage(int page, int pageSize){
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        orderService.page(ordersPage);
        return R.success(ordersPage);
    }
}